package mdm.com.sap;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;

import mdm.com.sap.model.Hu_Info;
import mdm.com.sap.model.Return_Result;
import mdm.com.sap.model.St_Item;
import mdm.com.sap.model.Tnote_Hu_TP;
import mdm.com.util.DateUtil;

public class Mod_FG {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(Mod_FG.class);
    
    public static Return_Result result ;
    
    /**
     * 判断HU是否在使用中
     * @param EXIDV HU号
     * @return
     */
    public static boolean ZBAPI_FGWH_CHECK_HU_INUSE(String EXIDV) {
        boolean res=false;
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_CHECK_HU_INUSE");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("EXIDV", SapUtil.ForNum(EXIDV,20));
            function.execute(RfcManager.getDestination());
            result = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            switch(result.getT()) {
                case "S":
                    LOGGER.info("=============message===="+result.getMESSAGE());
                    res = true;
                    break;
                default:
                    break;
            }
            
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    /**
     * 根据HU号查询HU信息
     * @param _TargetEXDIVs   HU号list集合
     * @return
     */
    public static List<Hu_Info> ZBAPI_GET_DATA_BY_HU(List<String> _TargetEXDIVs){
        List<Hu_Info> list=new ArrayList<Hu_Info>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_GET_DATA_BY_HU");
            JCoParameterList importParam = function.getImportParameterList();
            if(_TargetEXDIVs.size()>0) {
                JCoTable I_HU_NO = importParam.getTable("I_HU_NO");
                for(String str : _TargetEXDIVs) {
                    I_HU_NO.appendRow();
                    I_HU_NO.setValue("SIGN", "I");
                    I_HU_NO.setValue("OPTION", "EQ");
                    I_HU_NO.setValue("LOW",SapUtil.ForNum(str,20));
                    I_HU_NO.setValue("HIGH", SapUtil.ForNum(str,20));
                }
            }
            function.execute(RfcManager.getDestination());
            JCoTable ZRETURN=function.getTableParameterList().getTable("ZRETURN");
            Return_Result res=SapUtil.GetResult(ZRETURN);
            LOGGER.info("type="+res.getT());
            LOGGER.info("message="+res.getMESSAGE());
            switch (res.getT()) {
                case "S":
                case "":
                    JCoTable T_HU_DATA = function.getTableParameterList().getTable("T_HU_DATA");
                    for (int j = 0; j < T_HU_DATA.getNumRows(); j++) {
                        T_HU_DATA.setRow(j);
                        Hu_Info info=new Hu_Info();
                        info.setExidv2(T_HU_DATA.getString("EXIDV2"));
                        info.setSonum(T_HU_DATA.getString("SONUM"));
                        info.setVbeln(T_HU_DATA.getString("VBELN"));
                        info.setPosnr(T_HU_DATA.getString("POSNR"));
                        info.setVpobjkey(T_HU_DATA.getString("VPOBJKEY"));
                        info.setMatnr(T_HU_DATA.getString("MATNR"));
                        info.setJ_3asize(T_HU_DATA.getString("J_3ASIZE"));
                        info.setWerks(T_HU_DATA.getString("WERKS"));
                        info.setCharg(T_HU_DATA.getString("CHARG"));
                        info.setKalab(T_HU_DATA.getString("KALAB"));
                        info.setKains(T_HU_DATA.getString("KAINS"));
                        info.setMeins(T_HU_DATA.getString("MSEH3"));
                        info.setLgort(T_HU_DATA.getString("LGORT"));
                        info.setBstkd(T_HU_DATA.getString("BSTKD"));
                        info.setKunnr(T_HU_DATA.getString("KUNNR"));
                        info.setExidv(T_HU_DATA.getString("EXIDV"));
                        info.setTnote_number(T_HU_DATA.getString("TNOTE_NUMBER"));
                        info.setKdmat(T_HU_DATA.getString("KDMAT"));
                        info.setMaktx(T_HU_DATA.getString("MAKTX"));
                        info.setStatus(T_HU_DATA.getString("STATUS"));
                        info.setName1(T_HU_DATA.getString("NAME1"));
                        info.setAufnr(T_HU_DATA.getString("AUFNR"));
                        info.setVemng(T_HU_DATA.getString("VEMNG"));
                        info.setIsreturn(T_HU_DATA.getString("ISRETURN"));
                        info.setUsable(true);
                        info.setBrgew(T_HU_DATA.getString("BRGEW"));
                        info.setNtgew(T_HU_DATA.getString("NTGEW"));
                        list.add(info);
                    }
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    
    /**
     * 根据HU号查询HU信息
     * @param EXIDV   HU号
     * @return  list Hu_Info 集合
     */
    public static List<Hu_Info> ZBAPI_GET_DATA_BY_HU(String EXIDV,String user_type){
        List<Hu_Info> list=new ArrayList<Hu_Info>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_GET_DATA_BY_HU");
            JCoParameterList importParam = function.getImportParameterList();
            
            importParam.setValue("USER_TYPE", user_type);
            
            JCoTable I_HU_NO = importParam.getTable("I_HU_NO");
            I_HU_NO.appendRow();
            I_HU_NO.setValue("SIGN", "I");
            I_HU_NO.setValue("OPTION", "EQ");
            I_HU_NO.setValue("LOW",SapUtil.ForNum(EXIDV,20));
            I_HU_NO.setValue("HIGH", SapUtil.ForNum(EXIDV,20));
            function.execute(RfcManager.getDestination());
            JCoTable ZRETURN=function.getTableParameterList().getTable("ZRETURN");
            result =SapUtil.GetResult(ZRETURN);
            LOGGER.info("----type----"+result.getT());
            LOGGER.info("----message----"+result.getMESSAGE());
            switch (result.getT()) {
                case "S":
                case "":
                    JCoTable T_HU_DATA = function.getTableParameterList().getTable("T_HU_DATA");
                    for (int j = 0; j < T_HU_DATA.getNumRows(); j++) {
                        T_HU_DATA.setRow(j);
                        Hu_Info info=new Hu_Info();
                        info.setExidv2(T_HU_DATA.getString("EXIDV2"));
                        info.setSonum(T_HU_DATA.getString("SONUM"));
                        info.setVbeln(T_HU_DATA.getString("VBELN"));
                        info.setPosnr(T_HU_DATA.getString("POSNR"));
                        info.setVpobjkey(T_HU_DATA.getString("VPOBJKEY"));
                        info.setMatnr(T_HU_DATA.getString("MATNR"));
                        info.setJ_3asize(T_HU_DATA.getString("J_3ASIZE"));
                        info.setWerks(T_HU_DATA.getString("WERKS"));
                        info.setCharg(T_HU_DATA.getString("CHARG"));
                        info.setKalab(T_HU_DATA.getString("KALAB"));
                        info.setKains(T_HU_DATA.getString("KAINS"));
                        info.setMeins(T_HU_DATA.getString("MSEH3"));
                        info.setLgort(T_HU_DATA.getString("LGORT"));
                        info.setBstkd(T_HU_DATA.getString("BSTKD"));
                        info.setKunnr(T_HU_DATA.getString("KUNNR"));
                        info.setExidv(T_HU_DATA.getString("EXIDV"));
                        info.setTnote_number(T_HU_DATA.getString("TNOTE_NUMBER"));
                        info.setKdmat(T_HU_DATA.getString("KDMAT"));
                        info.setMaktx(T_HU_DATA.getString("MAKTX"));
                        info.setStatus(T_HU_DATA.getString("STATUS"));
                        info.setName1(T_HU_DATA.getString("NAME1"));
                        info.setAufnr(T_HU_DATA.getString("AUFNR"));
                        info.setVemng(T_HU_DATA.getString("VEMNG"));
                        info.setIsreturn(T_HU_DATA.getString("ISRETURN"));
                        info.setUsable(true);
                        info.setBrgew(T_HU_DATA.getString("BRGEW"));
                        info.setNtgew(T_HU_DATA.getString("NTGEW"));
                        list.add(info);
                    }
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    
    /**
     * 查询HU信息 带有装箱号信息
     * @param EXIDV  HU号
     * @return
     */
    public static List<Hu_Info> ZBAPI_GET_DATA_BY_HU2(String EXIDV,String user_type){
        List<Hu_Info> list=new ArrayList<Hu_Info>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_GET_DATA_BY_HU2");
            JCoParameterList importParam = function.getImportParameterList();
            
            
            importParam.setValue("USER_TYPE", user_type);
            
            JCoTable hu_no = importParam.getTable("I_HU_NO");
            hu_no.appendRow();
            hu_no.setValue("SIGN", "I");
            hu_no.setValue("OPTION", "EQ");
            hu_no.setValue("LOW",SapUtil.ForNum(EXIDV,20));
            hu_no.setValue("HIGH", SapUtil.ForNum(EXIDV,20));
            
            function.execute(RfcManager.getDestination());
            
            JCoTable ZRETURN=function.getTableParameterList().getTable("ZRETURN");
            Return_Result res=SapUtil.GetResult(ZRETURN);
            LOGGER.info("TYPE="+res.getT());
            LOGGER.info("message="+res.getMESSAGE());
            switch (res.getT()) {
                case "S":
                case "":
                    JCoTable T_HU_DATA = function.getTableParameterList().getTable("T_HU_DATA");
                    for (int j = 0; j < T_HU_DATA.getNumRows(); j++) {
                        T_HU_DATA.setRow(j);
                        Hu_Info info=new Hu_Info();
                        info.setExidv2(T_HU_DATA.getString("EXIDV2"));
                        info.setSonum(T_HU_DATA.getString("SONUM"));
                        info.setVbeln(T_HU_DATA.getString("VBELN"));
                        info.setPosnr(T_HU_DATA.getString("POSNR"));
                        info.setVpobjkey(T_HU_DATA.getString("VPOBJKEY"));
                        info.setMatnr(T_HU_DATA.getString("MATNR"));
                        info.setJ_3asize(T_HU_DATA.getString("J_3ASIZE"));
                        info.setWerks(T_HU_DATA.getString("WERKS"));
                        info.setCharg(T_HU_DATA.getString("CHARG"));
                        info.setKalab(T_HU_DATA.getString("KALAB"));
                        info.setKains(T_HU_DATA.getString("KAINS"));
                        info.setMeins(T_HU_DATA.getString("MSEH3"));
                        info.setLgort(T_HU_DATA.getString("LGORT"));
                        info.setBstkd(T_HU_DATA.getString("BSTKD"));
                        info.setKunnr(T_HU_DATA.getString("KUNNR"));
                        info.setExidv(T_HU_DATA.getString("EXIDV"));
                        info.setTnote_number(T_HU_DATA.getString("TNOTE_NUMBER"));
                        info.setKdmat(T_HU_DATA.getString("KDMAT"));
                        info.setMaktx(T_HU_DATA.getString("MAKTX"));
                        info.setStatus(T_HU_DATA.getString("STATUS"));
                        info.setName1(T_HU_DATA.getString("NAME1"));
                        info.setAufnr(T_HU_DATA.getString("AUFNR"));
                        info.setVemng(T_HU_DATA.getString("VEMNG"));
                        info.setIsreturn(T_HU_DATA.getString("ISRETURN"));
                        info.setUsable(true);
                        info.setBrgew(T_HU_DATA.getString("BRGEW"));
                        info.setNtgew(T_HU_DATA.getString("NTGEW"));
                        info.setPlNo(T_HU_DATA.getString("PLNO"));//装箱单
//                        info.setSum(T_HU_DATA.getString("SUM"));//箱数
                        System.out.println(T_HU_DATA.getValue("SUM"));
                        info.setSum(T_HU_DATA.getValue("SUM").toString());//箱数
                        list.add(info);
                    }
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    /**
     * 根据HU号查询HU信息
     * @param _TargetEXDIVs   HU号list集合
     * @param user_type   user_type
     * @return
     */
    public static List<Hu_Info> ZBAPI_GET_DATA_BY_HU(List<String> _TargetEXDIVs,String user_type){
        List<Hu_Info> list=new ArrayList<Hu_Info>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_GET_DATA_BY_HU");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("USER_TYPE", user_type);
            if(_TargetEXDIVs.size()>0) {
                JCoTable I_HU_NO = importParam.getTable("I_HU_NO");
                for(String str : _TargetEXDIVs) {
                    I_HU_NO.appendRow();
                    I_HU_NO.setValue("SIGN", "I");
                    I_HU_NO.setValue("OPTION", "EQ");
                    I_HU_NO.setValue("LOW",SapUtil.ForNum(str,20));
                    I_HU_NO.setValue("HIGH", SapUtil.ForNum(str,20));
                }
            }
            function.execute(RfcManager.getDestination());
            JCoTable ZRETURN=function.getTableParameterList().getTable("ZRETURN");
            Return_Result res=SapUtil.GetResult(ZRETURN);
            switch (res.getT()) {
                case "S":
                case "":
                    JCoTable T_HU_DATA = function.getTableParameterList().getTable("T_HU_DATA");
                    for (int j = 0; j < T_HU_DATA.getNumRows(); j++) {
                        T_HU_DATA.setRow(j);
                        Hu_Info info=new Hu_Info();
                        info.setExidv2(T_HU_DATA.getString("EXIDV2"));
                        info.setSonum(T_HU_DATA.getString("SONUM"));
                        info.setVbeln(T_HU_DATA.getString("VBELN"));
                        info.setPosnr(T_HU_DATA.getString("POSNR"));
                        info.setVpobjkey(T_HU_DATA.getString("VPOBJKEY"));
                        info.setMatnr(T_HU_DATA.getString("MATNR"));
                        info.setJ_3asize(T_HU_DATA.getString("J_3ASIZE"));
                        info.setWerks(T_HU_DATA.getString("WERKS"));
                        info.setCharg(T_HU_DATA.getString("CHARG"));
                        info.setKalab(T_HU_DATA.getString("KALAB"));
                        info.setKains(T_HU_DATA.getString("KAINS"));
                        info.setMeins(T_HU_DATA.getString("MSEH3"));
                        info.setLgort(T_HU_DATA.getString("LGORT"));
                        info.setBstkd(T_HU_DATA.getString("BSTKD"));
                        info.setKunnr(T_HU_DATA.getString("KUNNR"));
                        info.setExidv(T_HU_DATA.getString("EXIDV"));
                        info.setTnote_number(T_HU_DATA.getString("TNOTE_NUMBER"));
                        info.setKdmat(T_HU_DATA.getString("KDMAT"));
                        info.setMaktx(T_HU_DATA.getString("MAKTX"));
                        info.setStatus(T_HU_DATA.getString("STATUS"));
                        info.setName1(T_HU_DATA.getString("NAME1"));
                        info.setAufnr(T_HU_DATA.getString("AUFNR"));
                        info.setVemng(T_HU_DATA.getString("VEMNG"));
                        info.setIsreturn(T_HU_DATA.getString("ISRETURN"));
                        info.setUsable(true);
                        info.setBrgew(T_HU_DATA.getString("BRGEW"));
                        info.setNtgew(T_HU_DATA.getString("NTGEW"));
                        list.add(info);
                    }
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    /**
     * 查询HU基本信息
     * 
     * @param EXIDV  HU号
     * @return Hu_Info 转移单号
     */
    public static Hu_Info ZBAPI_GET_DATA_BY_HU_yz(String EXIDV) {
        Hu_Info info=new Hu_Info();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_GET_DATA_BY_HU");
            JCoParameterList importParam = function.getImportParameterList();
            JCoTable I_HU_NO = importParam.getTable("I_HU_NO");
            I_HU_NO.appendRow();
            I_HU_NO.setValue("SIGN", "I");
            I_HU_NO.setValue("OPTION", "EQ");
            I_HU_NO.setValue("LOW",SapUtil.ForNum(EXIDV,20));
            I_HU_NO.setValue("HIGH", SapUtil.ForNum(EXIDV,20));
            function.execute(RfcManager.getDestination());
            JCoTable ZRETURN=function.getTableParameterList().getTable("ZRETURN");
            Return_Result res=SapUtil.GetResult(ZRETURN);
            switch (res.getT()) {
                case "S":
                case "":
                    JCoTable T_HU_DATA = function.getTableParameterList().getTable("T_HU_DATA");
                    for (int j = 0; j < T_HU_DATA.getNumRows(); j++) {
                        T_HU_DATA.setRow(j);
                        info.setTnote_number(T_HU_DATA.getString("TNOTE_NUMBER"));
                        break;
                    }
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return info;
    }
    
    /**
     * 查询HU基本信息
     * 
     * @param EXIDV  HU号
     * @return
     */
    public static Hu_Info ZBAPI_FGWH_GET_HU_INFO(String EXIDV){
        Hu_Info info=new Hu_Info();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_GET_HU_INFO");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("EXIDV", SapUtil.ForNum(EXIDV,20));
            importParam.setValue("SKIP_STATUS_CHECK", "");
            function.execute(RfcManager.getDestination());
            Return_Result res=SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            LOGGER.info("ZBAPI_FGWH_GET_HU_INFO=="+res.getT());
            switch(res.getT()) {
                case "S":
                case "":
                    JCoStructure HU_INFO = function.getExportParameterList().getStructure("HU_INFO");
                    info.setTnote_number(HU_INFO.getString("TNOTE_NUMBER"));
                    info.setBstkd(HU_INFO.getString("BSTKD"));
                    info.setExidv(HU_INFO.getString("EXIDV"));
                    info.setJ_3asize(HU_INFO.getString("J_3ASIZE"));
                    info.setKdmat(HU_INFO.getString("KDMAT"));
                    info.setMatnr(HU_INFO.getString("MATNR"));
                    info.setPosnr(HU_INFO.getString("POSNR"));
                    info.setVbeln(HU_INFO.getString("VBELN"));
                    info.setVemeh(HU_INFO.getString("VEMEH"));
                    info.setVemng(HU_INFO.getString("VEMNG"));
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return info;
    }


    /**
     * 根据转移单号获取仓位信息
     * @param tnote_number  转移单号
     * @return  仓位
     */
    public static String ZBAPI_FGWH_GET_TNOTE_DEF_LOC(String tnote_number) {
        LOGGER.info("=====ZBAPI_FGWH_GET_TNOTE_DEF_LOC======");
        String pLocation = "";
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_GET_TNOTE_DEF_LOC");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("TNOTE_NUMBER", tnote_number);
            function.execute(RfcManager.getDestination());
            
            Return_Result res=SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            LOGGER.info("==type==="+res.getT());
            LOGGER.info("==message==="+res.getMESSAGE());
            switch(res.getT()) {
                case "S":
                case "":
                    pLocation = function.getExportParameterList().getString("DEFAULT_LOCATION");
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return pLocation;
    }
    
    
    
    /**
     * 保存收货基础信息查询
     * @param tnote_number  转移单号
     * @param pLocation  仓位
     * @return  Tnote_Hu_TP list 集合
     */
    public static List<Tnote_Hu_TP> ZBAPI_FGWH_GET_TNOTE_FOR_TP2(String tnote_number,String pLocation) {
        LOGGER.info("----------ZBAPI_FGWH_GET_TNOTE_FOR_TP2--------TNOTE_NUMBER{}-LOCATION{}--"+tnote_number+" || "+pLocation);
        List<Tnote_Hu_TP> list =  new ArrayList<Tnote_Hu_TP>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_GET_TNOTE_FOR_TP2");
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("TNOTE_NUMBER", tnote_number);
            importParam.setValue("LOCATION", pLocation);
            function.execute(RfcManager.getDestination());
            
            result = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            LOGGER.info("==type==="+result.getT());
            LOGGER.info("==message==="+result.getMESSAGE());
            
            switch(result.getT()) {
                case "S":
                case "":
                    JCoTable _TNOTE_HU = function.getExportParameterList().getTable("TNOTE_HU_DETAIL");
                    for(int j=0;j<_TNOTE_HU.getNumRows();j++) {
                        _TNOTE_HU.setRow(j);
                        Tnote_Hu_TP _tmp_TNOTE_HU_TP = new Tnote_Hu_TP();
                        _tmp_TNOTE_HU_TP.setBSTKD(_TNOTE_HU.getString("BSTKD"));
                        _tmp_TNOTE_HU_TP.setCHARG (_TNOTE_HU.getString("CHARG"));
                        _tmp_TNOTE_HU_TP.setCO_J_3ASIZE (_TNOTE_HU.getString("CO_J_3ASIZE"));
                        _tmp_TNOTE_HU_TP.setCO_MATNR (_TNOTE_HU.getString("CO_MATNR"));
                        _tmp_TNOTE_HU_TP.setCO_POSNR (_TNOTE_HU.getString("CO_POSNR"));
                        _tmp_TNOTE_HU_TP.setCO_SCHEDULE_LINE (_TNOTE_HU.getString("CO_SCHEDULE_LINE"));
                        _tmp_TNOTE_HU_TP.setCREATEDBY (_TNOTE_HU.getString("CREATEDBY"));
                        _tmp_TNOTE_HU_TP.setCREATIONDATE (_TNOTE_HU.getString("CREATIONDATE"));
                        _tmp_TNOTE_HU_TP.setCUSTOMER_BARCODE (_TNOTE_HU.getString("CUSTOMER_BARCODE"));
                        _tmp_TNOTE_HU_TP.setCUSTOMER_BOX (_TNOTE_HU.getString("CUSTOMER_BOX"));
                        _tmp_TNOTE_HU_TP.setDIFF_GRID_VALUE (_TNOTE_HU.getString("DIFF_GRID_VALUE"));
                        _tmp_TNOTE_HU_TP.setDIFF_PART_NO (_TNOTE_HU.getString("DIFF_PART_NO"));
                        _tmp_TNOTE_HU_TP.setDIFF_UOM (_TNOTE_HU.getString("DIFF_UOM"));
                        _tmp_TNOTE_HU_TP.setEXIDV (_TNOTE_HU.getString("EXIDV"));
                        _tmp_TNOTE_HU_TP.setH_CREATEDBY (_TNOTE_HU.getString("H_CREATEDBY"));
                        _tmp_TNOTE_HU_TP.setH_CREATIONDATE (_TNOTE_HU.getString("H_CREATIONDATE"));
                        _tmp_TNOTE_HU_TP.setH_LASTCHANGEDBY (_TNOTE_HU.getString("H_LASTCHANGEDBY"));
                        _tmp_TNOTE_HU_TP.setH_LASTCHANGEDDATE (_TNOTE_HU.getString("H_LASTCHANGEDDATE"));
                        _tmp_TNOTE_HU_TP.setJ_3ASIZE (_TNOTE_HU.getString("J_3ASIZE"));
                        _tmp_TNOTE_HU_TP.setKDMAT (_TNOTE_HU.getString("KDMAT"));
                        _tmp_TNOTE_HU_TP.setKUNNR (_TNOTE_HU.getString("KUNNR"));
                        _tmp_TNOTE_HU_TP.setLASTCHANGEDBY (_TNOTE_HU.getString("LASTCHANGEDBY"));
                        _tmp_TNOTE_HU_TP.setLASTCHANGEDDATE (_TNOTE_HU.getString("LASTCHANGEDDATE"));
                        _tmp_TNOTE_HU_TP.setLGORT (_TNOTE_HU.getString("LGORT"));
                        _tmp_TNOTE_HU_TP.setMAKTX (_TNOTE_HU.getString("MAKTX"));
                        _tmp_TNOTE_HU_TP.setMATNR (_TNOTE_HU.getString("MATNR"));
                        _tmp_TNOTE_HU_TP.setNAME1 (_TNOTE_HU.getString("NAME1"));
                        _tmp_TNOTE_HU_TP.setORDERNUMBER (_TNOTE_HU.getString("ORDERNUMBER"));
                        _tmp_TNOTE_HU_TP.setPOSNR (_TNOTE_HU.getString("POSNR"));
                        _tmp_TNOTE_HU_TP.setPSCODE (_TNOTE_HU.getString("PSCODE"));
                        _tmp_TNOTE_HU_TP.setRELEASE_DOCUMENT (_TNOTE_HU.getString("RELEASE_DOCUMENT"));
                        _tmp_TNOTE_HU_TP.setRM_BOXNO (_TNOTE_HU.getString("RM_BOXNO"));
                        _tmp_TNOTE_HU_TP.setSEL (_TNOTE_HU.getString("SEL"));
                        _tmp_TNOTE_HU_TP.setSTATUS (_TNOTE_HU.getString("STATUS"));
                        _tmp_TNOTE_HU_TP.setTNOTE_ISTATUS (_TNOTE_HU.getString("TNOTE_ISTATUS"));
                        _tmp_TNOTE_HU_TP.setTNOTE_NUMBER (_TNOTE_HU.getString("TNOTE_NUMBER"));
                        _tmp_TNOTE_HU_TP.setTNOTE_STATUS (_TNOTE_HU.getString("TNOTE_STATUS"));
                        _tmp_TNOTE_HU_TP.setV_MATNR (_TNOTE_HU.getString("V_MATNR"));
                        _tmp_TNOTE_HU_TP.setV_VRKME (_TNOTE_HU.getString("V_VRKME"));
                        _tmp_TNOTE_HU_TP.setVBELN (_TNOTE_HU.getString("VBELN"));
                        _tmp_TNOTE_HU_TP.setVEMEH (_TNOTE_HU.getString("VEMEH"));
                        _tmp_TNOTE_HU_TP.setVEMNG (_TNOTE_HU.getString("VEMNG"));
                        _tmp_TNOTE_HU_TP.setVENUM (_TNOTE_HU.getString("VENUM"));
                        _tmp_TNOTE_HU_TP.setVEPOS (_TNOTE_HU.getString("VEPOS"));
                        _tmp_TNOTE_HU_TP.setVKGRP (_TNOTE_HU.getString("VKGRP"));
                        _tmp_TNOTE_HU_TP.setWERKS (_TNOTE_HU.getString("WERKS"));
                        _tmp_TNOTE_HU_TP.setZZMATNR (_TNOTE_HU.getString("ZZMATNR"));
                        _tmp_TNOTE_HU_TP.setZZPOSNR (_TNOTE_HU.getString("ZZPOSNR"));
                        _tmp_TNOTE_HU_TP.setZZVBELN (_TNOTE_HU.getString("ZZVBELN"));
                        _tmp_TNOTE_HU_TP.setZZVRKME (_TNOTE_HU.getString("ZZVRKME"));
                        list.add(_tmp_TNOTE_HU_TP);
                    }
                    break;
                default:
                    break;
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    
    /**
     * 自动DN 根据包装清单号查询
     * @param pPackingID  包装清单号
     * @return
     */
    public static List<Hu_Info> ZBAPI_PACKING_LIST_STATUS(String pPackingID){
        List<Hu_Info> list =new ArrayList<Hu_Info>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_PACKING_LIST_STATUS");
            JCoTable PACKING_LIST = function.getImportParameterList().getTable("PACKING_LIST");
            PACKING_LIST.appendRow();
            PACKING_LIST.setValue("PACKING_LIST_ID", pPackingID);
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("PACKING_LIST", PACKING_LIST);
            function.execute(RfcManager.getDestination());
            result = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            LOGGER.info("==type==="+result.getT());
            LOGGER.info("==message==="+result.getMESSAGE());
            
            switch(result.getT()) {
                case "E":
                    break;
                default:
                    JCoTable OUT_T_OUTPUT = function.getExportParameterList().getTable("PACKING_LIST_STATUS");
                    for (int i = 0; i < OUT_T_OUTPUT.getNumRows(); i++) {
                        OUT_T_OUTPUT.setRow(i);
                        Hu_Info huinfo = new Hu_Info();
                        huinfo.setPlNo(OUT_T_OUTPUT.getString("PACKING_LIST_ID"));
                        huinfo.setStatus(OUT_T_OUTPUT.getString("CHECK_STATUS"));
                        list.add(huinfo);
                    }
            }
        } catch (JCoException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
       return list;
    }
    
    
    
    /**
     * 根据包装清单号查询HU号
     * @param pPackingID  包装清单号List集合
     * @param pUserType   用户类型
     * @return
     */
    public static List<Hu_Info> ZBAPI_PACKING_LIST_AND_HU(List<String> pPackingID,String pUserType){
        List<Hu_Info> list =new ArrayList<Hu_Info>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_PACKING_LIST_AND_HU");
            JCoTable PACKING_LIST = function.getImportParameterList().getTable("PACKING_LIST");
            for(String str : pPackingID) {
                PACKING_LIST.appendRow();
                PACKING_LIST.setValue("PACKING_LIST_ID", str);
            }
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("PACKING_LIST", PACKING_LIST);
            importParam.setValue("USER_TYPE", pUserType);
            function.execute(RfcManager.getDestination());
            result = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            LOGGER.info("==type==="+result.getT());
            LOGGER.info("==message==="+result.getMESSAGE());
            switch(result.getT()) {
                case "E":
                    break;
                default:
                    JCoTable OUT_T_OUTPUT = function.getExportParameterList().getTable("PACKING_LIST_HU");
                    for (int i = 0; i < OUT_T_OUTPUT.getNumRows(); i++) {
                        OUT_T_OUTPUT.setRow(i);
                        Hu_Info huinfo = new Hu_Info();
                        huinfo.setPlNo(OUT_T_OUTPUT.getString("PACKING_LIST_ID"));
                        huinfo.setExidv(OUT_T_OUTPUT.getString("HANDLING_UNIT"));
                        list.add(huinfo);
                    }
            }
        } catch (JCoException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return list;
    }
    
    /**
     * 创建DN
     * @param pPackingID
     * @param pUserType
     * @param barcodeUser
     * @param DELIVERY_DATE
     * @param DN_DOCUMENT_DATE
     * @param ACTUAL_GI_DATE
     * @return
     */
    public static List<Hu_Info> ZBAPI_PL_CREATE_DN_AND_PGI(List<String> pPackingID,String pUserType,String barcodeUser,String DELIVERY_DATE, String DN_DOCUMENT_DATE, String ACTUAL_GI_DATE){
        List<Hu_Info> list =new ArrayList<Hu_Info>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_PL_CREATE_DN_AND_PGI");
            JCoTable PACKING_LIST = function.getImportParameterList().getTable("PACKING_LIST");
            for(String str : pPackingID) {
                PACKING_LIST.appendRow();
                PACKING_LIST.setValue("PACKING_LIST_ID", str);
            }
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("PACKING_LIST", PACKING_LIST);
            importParam.setValue("DELIVERY_DATE", DELIVERY_DATE);
            importParam.setValue("DN_DOCUMENT_DATE", DN_DOCUMENT_DATE);
            importParam.setValue("ACTUAL_GI_DATE", ACTUAL_GI_DATE);
            importParam.setValue("BARCODE_USER", barcodeUser);
            
            
            importParam.setValue("USER_TYPE", pUserType);
            function.execute(RfcManager.getDestination());
            JCoTable outreturn = function.getExportParameterList().getTable("RETURN");
            for (int i = 0; i < outreturn.getNumRows(); i++) {
                outreturn.setRow(i);
                Hu_Info huinfo = new Hu_Info();
                huinfo.setPlNo(outreturn.getString("PACKING_LIST_ID"));
                huinfo.setDn(outreturn.getString("DELIVERY_NUMBER"));
                huinfo.setType(outreturn.getString("TYPE"));
                huinfo.setMessage(outreturn.getString("MESSAGE"));
                list.add(huinfo);
            }
        } catch (JCoException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return list;
    }
    
    /**
     * 打印DN
     * @param DeliveryNo  dn号list集合
     * @param barcodeUser  用户名
     * @return
     */
    public static Return_Result ZBAPI_EMAIL_DN_BARCODE(List<String> DeliveryNo,String barcodeUser){
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_EMAIL_DN_BARCODE");
            JCoTable DELIVERY_NUMBER = function.getImportParameterList().getTable("DELIVERY_NUMBER");
            for(String dn : DeliveryNo) {
                DELIVERY_NUMBER.appendRow();
                DELIVERY_NUMBER.setValue("DELIVERY_NUMBER", dn);
            }
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("DELIVERY_NUMBER", DELIVERY_NUMBER);
            importParam.setValue("BARCODE_USER", barcodeUser);
            function.execute(RfcManager.getDestination());
            result = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            LOGGER.info("==type==="+result.getT());
            LOGGER.info("==message==="+result.getMESSAGE());
        } catch (JCoException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return result;
    }
    
    
    /**
     * 内厂棉杯收货
     * @param pHuNo    HU号 集合
     * @param listhuinfo  HU号基本信息
     * @param pLocation   仓位
     * @param user_type   用户类型
     * @return 
     */
    public static Return_Result ZBAPI_FGWH_HU_GOODS_MOV_2A(List<String> pHuNo,List<Hu_Info> listhuinfo,String pLocation,String user_type) {
        Return_Result res = null;
        try {
            
            JCoFunction function = RfcManager.getFunction("ZBAPI_FGWH_GET_ORDER_FOR_TP2");
            JCoTable TBL_EXIDV = function.getTableParameterList().getTable("EXIDV");
            JCoTable TBL_HU_ORDER = function.getTableParameterList().getTable("HU_ORDER");
            for(String HU_Number : pHuNo) {
                for(Hu_Info info : listhuinfo) {
                    System.out.println("info.getExidv()=="+info.getExidv()+"       HU_Number="+SapUtil.ForNum(HU_Number, 20));
                    if(info.getExidv().equals(SapUtil.ForNum(HU_Number, 20))) {
                        TBL_EXIDV.appendRow();
                        TBL_EXIDV.setValue("EXIDV", SapUtil.ForNum(HU_Number, 20));
                        TBL_EXIDV.setValue("VEPOS", info.getVepos());
                        TBL_EXIDV.setValue("SONUM", info.getSonum());
                        TBL_EXIDV.setValue("AUFNR", info.getAufnr());
                        
                        TBL_HU_ORDER.appendRow();
                        TBL_HU_ORDER.setValue("EXIDV", SapUtil.ForNum(HU_Number, 20));
                        TBL_HU_ORDER.setValue("VEPOS", info.getVepos());
                        TBL_HU_ORDER.setValue("SONUM", info.getSonum());
                        TBL_HU_ORDER.setValue("AUFNR", info.getAufnr());
                    }
                }
            }
            
            function.execute(RfcManager.getDestination());
            
            JCoTable _HU_ORDER_DETAIL = function.getTableParameterList().getTable("HU_ORDER_DETAIL");
            
            JCoFunction BapiFunction = RfcManager.getFunction("ZBAPI_FGWH_HU_GOODS_MOV_2A");
            BapiFunction.getImportParameterList().setValue("LOCATION", pLocation);
            BapiFunction.getImportParameterList().setValue("USER_TYPE", user_type);
            
            JCoTable HU_TBL = BapiFunction.getImportParameterList().getTable("HU");
            for(String HU_Number : pHuNo) {
                HU_TBL.appendRow();
                HU_TBL.setValue("EXIDV", SapUtil.ForNum(HU_Number, 20));
            }
           
            BapiFunction.getImportParameterList().setValue("HU_DETAIL", _HU_ORDER_DETAIL);
            
            BapiFunction.execute(RfcManager.getDestination());
            
            res = SapUtil.GetResult(BapiFunction.getExportParameterList().getTable("RETURN"));
            LOGGER.info("=========type======"+res.getT());
            LOGGER.info("=========message======"+res.getMESSAGE());
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    /**
     * 内厂棉杯转厂
     * @param pHuNo  HU号list集合
     * @param factory  工厂
     * @param whKo		         转入仓位
     * @param pUserType  用户类型
     * @return
     */
    public static Return_Result ZBAPI_MOVE_TO_PLANT_STORAGE_HU(List<String> pHuNo, String factory, String whKo, String pUserType) {
        Return_Result res = null;
        try {
            
            JCoFunction function = RfcManager.getFunction("ZBAPI_MOVE_TO_PLANT_STORAGE_HU");
            JCoTable ET_EXIDV = function.getTableParameterList().getTable("ET_EXIDV");
            for(String hu : pHuNo) {
                ET_EXIDV.appendRow();
                ET_EXIDV.setValue("EXIDV", SapUtil.ForNum(hu, 20));
            }
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("IV_BKTXT", "12345");
            importParam.setValue("IV_WERKS", factory);
            importParam.setValue("IV_LGORT", whKo);
            importParam.setValue("USER_TYPE", pUserType);
            importParam.setValue("IV_BLDAT", DateUtil.formatDates(new Date()));
            importParam.setValue("IV_BUDAT", DateUtil.formatDates(new Date()));
            
            function.execute(RfcManager.getDestination());
            
            res = SapUtil.GetResult(function.getTableParameterList().getTable("ZRETURN"));
            LOGGER.info("=========type======"+res.getT());
            LOGGER.info("=========message======"+res.getMESSAGE());
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return res;
    }
    
    
    /**
     * 根据盘点单号和仓位 查询
     * @param pBarcode
     * @param pLocationID
     * @return
     */
    public static List<St_Item> GetStockTakeItem(String pBarcode, String pLocationID){
        List<St_Item> list =new ArrayList<St_Item>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_BARCODE_STOCKTAKE");
            
            JCoParameterList importParam = function.getImportParameterList();
            importParam.setValue("IBLNR", pBarcode.toUpperCase());
            importParam.setValue("LGORT", pLocationID);
            importParam.setValue("FLAG", " ");
            function.execute(RfcManager.getDestination());
            result = SapUtil.GetResult(function.getExportParameterList().getStructure("RETURN"));
            LOGGER.info("==type==="+result.getT());
            LOGGER.info("==message==="+result.getMESSAGE());
            switch(result.getT()) {
                case "E":
                    break;
                default:
                    JCoTable codetable = function.getTableParameterList().getTable("BAR_CODE_INVENTORY");
                    for (int i = 0; i < codetable.getNumRows(); i++) {
                        codetable.setRow(i);
                        St_Item item = new St_Item();
                        item.setIblnr(codetable.getString("IBLNR"));
                        item.setIbort(codetable.getString("IBORT"));
                        item.setIbqty(codetable.getString("IBQTY"));
                        item.setSernr(codetable.getString("SERNR"));
                        item.setMatnr(codetable.getString("MATNR"));
                        item.setJ_3asize(codetable.getString("J_3ASIZE"));
                        item.setNwqty("0");
                        list.add(item);
                    }
            }
        } catch (JCoException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return list;
    }
}
